import 'package:flutter/material.dart';
import './client_dio.dart';

// Arguments for editing a pack
class PackArguments {
  final int id;
  final String name;

  PackArguments(this.id, this.name);
}

// Screen for editing a pack
class PackArgumentsScreen extends StatelessWidget {
  const PackArgumentsScreen({super.key});

  static const routeName = '/extractPackArguments';

  @override
  Widget build(BuildContext context) {
    // Extract the arguments from the current ModalRoute settings
    final args = ModalRoute.of(context)!.settings.arguments as PackArguments;
    final TextEditingController name_Controller = TextEditingController(text: args.name);

    return Scaffold(
      appBar: AppBar(
        title: Text("Edit Pack"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              controller: name_Controller,
              decoration: InputDecoration(labelText: "Pack Name"),
            ),
            ElevatedButton(
              onPressed: () {
                // Call the packUpdate API to update the pack
                packUpdate({
                  "id": args.id,
                  "name": name_Controller.text
                }, (r) {
                  print(r);
                  Navigator.of(context).pop(true); // Return true to indicate success
                }, (e) {
                  print(e);
                  Navigator.of(context).pop(false); // Return false to indicate failure
                });
              },
              child: Text("Save"),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).pop(); // Cancel editing
              },
              child: Text("Cancel"),
            ),
          ],
        ),
      ),
    );
  }
}

// Screen for creating a new pack
class CreatePackScreen extends StatelessWidget {
  const CreatePackScreen({super.key});

  static const routeName = '/createPack';

  @override
  Widget build(BuildContext context) {
    final TextEditingController name_Controller = TextEditingController(text: "");
    final TextEditingController bh_Controller = TextEditingController(text: "");

    return Scaffold(
      appBar: AppBar(
        title: Text("Create New Pack"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              controller: bh_Controller,
              decoration: InputDecoration(labelText: "BH"),
            ),
            TextField(
              controller: name_Controller,
              decoration: InputDecoration(labelText: "Pack Name"),
            ),
            ElevatedButton(
              onPressed: () {
                // Call the packCreate API to create a new pack
                packCreate({
                  "bh": bh_Controller.text,
                  "name": name_Controller.text
                }, (r) {
                  print(r);
                  Navigator.of(context).pop(true); // Return true to indicate success
                }, (e) {
                  print(e);
                  Navigator.of(context).pop(false); // Return false to indicate failure
                });
              },
              child: Text("Save"),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).pop(); // Cancel creation
              },
              child: Text("Cancel"),
            ),
          ],
        ),
      ),
    );
  }
}